﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogicLayer.Base;
using DALLayer.Providers;
using DataDefinition;
using Objects;

namespace BusinessLogicLayer.Manager
{
    public class VehicleModelTypeYearManager: ManagerBase<VehicleModelTypeYearDataProvider>
    {
       #region [Constructor]
         /// <summary>
        /// Initializes a new instance of the <see cref="VehicleModelTypeYearManager" /> class.
        /// </summary>
        public VehicleModelTypeYearManager() { }
        #endregion

        #region [Manager base class implementations]
        /// <summary>
        /// Pass the data provider to the class
        /// </summary>
        protected override void InitializeDataProvider()
        {
            //Pass the specific data provider to the manager class
            base.DataProvider = new VehicleModelTypeYearDataProvider();
        }

        #endregion

        #region [Public Methods]
        /// <summary>
        /// Update or insert a vehicle model type year.
        /// </summary>
        /// <param name="vehicleModelTypeYear">vehicle model type year object</param>
        /// <returns>true or false</returns>
        public bool UpdateVehicleModelTypeYear(VehicleModelTypeYear vehicleModelTypeYear)
        {
            bool success = false;
            try
            {
                //if there is an id
                if (vehicleModelTypeYear.VehicleModelTypeYearID > 0)
                {
                    //Update the vehicle model type year
                    success = base.DataProvider.UpdateVehicleModelTypeYear(vehicleModelTypeYear.VehicleModelTypeYearID, vehicleModelTypeYear.VehicleModelTypeID, vehicleModelTypeYear.VehicleModelTypeImageURL, vehicleModelTypeYear.VehicleYear, vehicleModelTypeYear.IsActive);
                }
                else
                {
                    //Insert the vehicle model type year
                    success = base.DataProvider.InsertVehicleModelTypeYear(vehicleModelTypeYear.VehicleModelTypeID, vehicleModelTypeYear.VehicleModelTypeImageURL, vehicleModelTypeYear.VehicleYear, vehicleModelTypeYear.IsActive);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            //returns true or false
            return success;

        }
        /// <summary>
        /// Gets all the years
        /// </summary>
        /// <returns>Vehicle model type year list</returns>
        public List<VehicleModelTypeYear> GetVehicleModelTypeYears()
        {
            //The user details
            List<VehicleModelTypeYear> vehicleModelTypeYearDetails = null;
            try
            {
                //Get the vehicle model type year details list
                vehicleModelTypeYearDetails = this.GetVehicleModelTypeYears(0, false);
            }
            catch (Exception eX)
            {
                //log error here
            }
            //The vehicle model type year list to return
            return vehicleModelTypeYearDetails;
        }
        /// <summary>
        ///  Gets the the vehicle model type year by id
        /// </summary>
        /// <param name="vehicleModelYearID">the user id</param>
        /// <returns>the vehicle model type year list</returns>
        public List<VehicleModelTypeYear> GetVehicleModelTypeYearsByID(int vehicleModelTypeYearID)
        {
            //The the vehicle model type year details
            List<VehicleModelTypeYear> vehicleModelTypeYearDetails = null;
            try
            {
                //Get the vehicle model type year details list
                vehicleModelTypeYearDetails = this.GetVehicleModelTypeYears(vehicleModelTypeYearID, true);
            }
            catch (Exception eX)
            {
                //log error here
            }
            //The the vehicle model type yeardetails list to return
            return vehicleModelTypeYearDetails;

        }
        /// <summary>
        /// Gets the model vehicle image
        /// </summary>
        /// <param name="vehicleModelTypeID">model type id</param>
        /// <returns>Vehicle model type image list</returns>
        public List<ModelTypeImage> GetVehicleModelTypeImage(int vehicleModelTypeID)
        {
            //The the model type image details
            List<ModelTypeImage> modelTypeImageDetails = null;
            try
            {
                //Get the data set returned from the data provider
                AutoFixDataSet dataSet = base.DataProvider.GetModelTypeYearImageDetails(vehicleModelTypeID);

                if (dataSet != null && dataSet.ModelTypeImage != null && dataSet.ModelTypeImage.Rows != null)
                {
                    //Set the returned filled list
                    modelTypeImageDetails = FillModelTypeImageObjectsByEntities(dataSet.ModelTypeImage);
                }
            }
            catch (Exception eX)
            {
                //log error here
            }
            //The the model type image details list to return
            return modelTypeImageDetails;

        }
        #endregion
        #region [Private Methods]
  
        private List<VehicleModelTypeYear> GetVehicleModelTypeYears(int userId, bool selectById)
        {
            //The vehicle model type year details to be returned
            List<VehicleModelTypeYear> vehicleModelTypeYearDetails = null;
            try
            {
                //Get the data set returned from the data provider
                AutoFixDataSet dataSet = base.DataProvider.GetModelYearDetails(userId, selectById);

                if (dataSet != null && dataSet.VehicleModelTypeYear != null && dataSet.VehicleModelTypeYear.Rows != null)
                {
                    //Set the returned filled list
                    vehicleModelTypeYearDetails = FillVehicleModelTypeYearObjectsByEntities(dataSet.VehicleModelTypeYear);
                }
            }
            catch (Exception eX)
            {
                //log the error here, dont throw the exception
            }
            //Return the user details list
            return vehicleModelTypeYearDetails;

        }
        /// <summary>
        /// <summary>
        /// Fill the vehicleModelTypeYear object
        /// </summary>
        /// <param name="dataTable">data table</param>
        /// <returns>the vehicle model type year list</returns>
        private List<VehicleModelTypeYear> FillVehicleModelTypeYearObjectsByEntities(AutoFixDataSet.VehicleModelTypeYearDataTable dataTable)
        {
            //The user list that contains the objects
            //Loop through the data table and create new VehicleModelTypeYearObject 
            //finally select the VehicleModelTypeYearObject and convert to list
            List<VehicleModelTypeYear> vehicleModelTypeYearDetails = (from vmtyo in
                                          (from vmty in dataTable.AsEnumerable<AutoFixDataSet.VehicleModelTypeYearRow>()
                                           select new
                                           {
                                               VehicleModelTypeYearObject = new VehicleModelTypeYear()
                                               {
                                                   VehicleModelTypeYearID = vmty.VehicleModelTypeYearID,
                                                   VehicleModelTypeID = vmty.VehicleModelTypeID,
                                                   VehicleModelTypeImageURL = (vmty.IsVehicleModelTypeImageURLNull()) ? null : vmty.VehicleModelTypeImageURL,
                                                   VehicleYear = vmty.VehicleYear,
                                                   IsActive = vmty.IsActive
                                               }
                                           })
                                            select vmtyo.VehicleModelTypeYearObject).ToList();
            //Return the filled user details list
            return vehicleModelTypeYearDetails;
        }
        /// <summary>
        /// Fill the ModelTypeImage object
        /// </summary>
        /// <param name="dataTable">data table</param>
        /// <returns>the model type year image list</returns>
        private List<ModelTypeImage> FillModelTypeImageObjectsByEntities(AutoFixDataSet.ModelTypeImageDataTable dataTable)
        {
            //The list that contains the objects
            //Loop through the data table and create new ModelTypeImageObject 
            //finally select the ModelTypeImageObject and convert to list
            List<ModelTypeImage> modelTypeImageDetails = (from mtio in
                                                                          (from mti in dataTable.AsEnumerable<AutoFixDataSet.ModelTypeImageRow>()
                                                                           select new
                                                                           {
                                                                               ModelTypeImageObject = new ModelTypeImage()
                                                                               {
                                                                                   VehicleModelTypeImageURL = (mti.IsVehicleModelTypeImageURLNull()) ? null : mti.VehicleModelTypeImageURL,
                                                                               
                                                                               }
                                                                           })
                                                                select mtio.ModelTypeImageObject).ToList();
            //Return the filled model Type Image details list
            return modelTypeImageDetails;
        }
        #endregion
    }
}
